import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:simple_pwd_node/entity/record.dart';
import 'package:simple_pwd_node/components/fold_view.dart';
import 'package:simple_pwd_node/model/record_model.dart';
import 'package:simple_pwd_node/pages/add_new.dart';

import 'package:simple_pwd_node/pages/setting.dart';
import 'package:simple_pwd_node/pages/import_pwd.dart';
import 'package:simple_pwd_node/pages/output_pwd.dart';

class Home extends StatefulWidget {
  Home({Key key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
  
  int _openIndex = -1;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('记个密码'),
      ),
      drawer: Drawer(
        child: ListView(
          children: <Widget>[
            new UserAccountsDrawerHeader(
              accountName: Text("记个密码"),
              accountEmail: Text("flutter@gmail.com"),
              currentAccountPicture: new GestureDetector(
                child: new CircleAvatar(
                  backgroundImage: new ExactAssetImage("images/header_icon.jpg"),
                ),
              ),
              decoration: new BoxDecoration(
                image: new DecorationImage(
                  fit: BoxFit.fill,
                  image: new ExactAssetImage("images/header_bg.jpg"),),),
            ),
            new ListTile(
              title: new Text("导入"),
              trailing: new Icon(Icons.input_sharp),
              onTap: (){
                Navigator.of(context).push(MaterialPageRoute(builder: (_){
                  return new ImportPwdPage();
                }));
              },
            ),
            new ListTile(
              title: new Text("导出"),
              trailing: new Icon(Icons.file_copy),
              onTap: (){
                Navigator.of(context).push(MaterialPageRoute(builder: (_){
                  return new OutputPwdPage();
                }));
              },
            ),
            new Divider(),
            new ListTile(
              title: new Text("设置"),
              trailing: new Icon(Icons.settings),
              onTap: (){
                Navigator.of(context).push(MaterialPageRoute(builder: (_){
                  return new SettingPage();
                }));
              },
            ),
          ],
        ),
      ),
      body: Container(
          color: Color(0xfff5f5f5),
          padding: EdgeInsets.symmetric(vertical: 0, horizontal: 5),
          child: Consumer<RecordModel>(
            builder: (BuildContext ctx,value,Widget child){
              return ListView.builder(
                  itemCount: value.recordList.length,
                  itemBuilder: (BuildContext ctx,int index){
                    return _listItems(ctx,index,value.recordList[index]);
                  }
              );
            },
          )
          ),
      floatingActionButton: FloatingActionButton(
          backgroundColor: Color(0xff6490c8),
          onPressed: () {
            Navigator.of(context).pushNamed('/addNew');
          },
          child: Icon(Icons.add)),
    );
  }

  Widget _listItems(BuildContext context, int index ,Record record) {
    return Container(
      margin: EdgeInsets.only(bottom: 10),
      padding: EdgeInsets.symmetric(vertical: 6, horizontal: 8),
      decoration: BoxDecoration(
          color: Color(0xffffffff), borderRadius: BorderRadius.circular(5.0)),
      child: FoldWidget(
        isFold: index == _openIndex,
        titleWidget: Flex(direction: Axis.horizontal, children: [
          Expanded(
            flex: 1,
            child: Container(color: Color(0x808080), child: Text(record.name,style: TextStyle(color: Colors.blue,fontSize: 18),)),
          ),
          Expanded(
            flex: 3,
            child: Container(
                child: Text(record.account,
                    style: TextStyle(
                        fontSize: 16))),
          ),
          Container(
              margin: EdgeInsets.only(right: 4),
              child: Icon(Icons.copy)),
          IconButton(
            icon: Icon(Icons.remove_red_eye),
            onPressed: () {
              setState(() {
                _openIndex = index;
              });
            },
          )
        ]),
        contentWidget: Container(
          width: double.infinity,
          padding: EdgeInsets.symmetric(vertical: 0, horizontal: 4),
          decoration: BoxDecoration(
              color: Color(0xfff5f5f5),
              borderRadius: BorderRadius.circular(5.0)),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(record.password,style: TextStyle(fontSize: 16),),
              IconButton(
                icon: Icon(Icons.edit,size: 20,),
                onPressed: (){
                  _editItem(record);
                },
              )
            ],
          )
        ),
      ),
    );
  }

  _editItem(Record item){
    Navigator.of(context).push(MaterialPageRoute(builder: (_){
      return new AddNewPage(
        mode: 'edit',
        editRecord: item,
      );
    }));
  }

  @override
  void dispose() {
    super.dispose();
  }
}
